package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.DishCash;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishCategory;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ICategoryService;
import com.itheima.service.IDishService;
import com.itheima.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/statics")
public class DishCashController {

    @Autowired
    ICategoryService categoryService;

    @Autowired
    IDishService dishService;

    @Autowired
    ISetmealService setmealService;




    @GetMapping("/dishCash")
    private Map DishCash(){
        //1.先查询每个菜品的分类
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,1);
        List<Category> categories = categoryService.list(wrapper);

        //2.查询每个菜对应的菜品
        List<Dish> dishList = dishService.list();


        Integer amount = 0;
        ArrayList<DishCategory> arrayList = new ArrayList<>();

        //3.再根据每个菜品进行统计每个菜品的金额
        for (Category category : categories) {
            for (Dish dish : dishList) {
                if (category.getId().equals(dish.getCategoryId())){
                    //说明是同一道菜，就将金额加起来
                    amount += dish.getPrice();
                }
            }
            //封装的value和Nam e
            DishCategory dishCategory = new DishCategory();
            dishCategory.setValue(amount);
            dishCategory.setName(category.getName());
             arrayList.add(dishCategory);
            //重新将amount置为0
            amount = 0;

        }

        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("dishCategoryCash",arrayList);

        return hashMap;
    }

    @GetMapping("/setmealCash")
    private Map setmealCash(){
        //1.先查询每个套餐的分类
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,2);
        List<Category> categories = categoryService.list(wrapper);

        //2.查询每个分类对应的套餐
        List<Setmeal> setMeals = setmealService.list();

        Integer amount = 0;
        ArrayList<DishCategory> arrayList = new ArrayList<>();

        //3.再根据每个菜品进行统计每个菜品的金额
        for (Category category : categories) {
            for (Setmeal setMeal : setMeals) {
                if (category.getId().equals(setMeal.getCategoryId())){
                    //说明是同一道菜，就将金额加起来
                    amount += setMeal.getPrice();
                }
            }
            //封装的value和Name
            DishCategory dishCategory = new DishCategory();
            dishCategory.setValue(amount);
            dishCategory.setName(category.getName());
            arrayList.add(dishCategory);
            //重新将amount置为0
            amount = 0;

        }

        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("setmealCash",arrayList);

        return hashMap;

    }

    @GetMapping("/dishNum")
    private Map dishNum(){
        //1.先查询每个菜品的分类
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,1);
        List<Category> categories = categoryService.list(wrapper);

        //2.查询每个菜对应的菜品
        List<Dish> dishList = dishService.list();
        ArrayList<DishCategory> arrayList = new ArrayList<>();

        Integer num = 0;

        for (Category category : categories) {
            for (Dish dish : dishList) {
                if (category.getId().equals(dish.getCategoryId())){
                    num ++;
                }
            }
            //封装的value和Name
            DishCategory dishCategory = new DishCategory();
            dishCategory.setValue(num);
            dishCategory.setName(category.getName());
            arrayList.add(dishCategory);
            //重新将amount置为0
            num = 0;
        }


        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("dishNum",arrayList);

        return hashMap;
    }

    @GetMapping("/setmealNum")
    private Map setmealNum(){
        //1.先查询每个套餐的分类
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,2);
        List<Category> categories = categoryService.list(wrapper);

        //2.查询每个分类对应的套餐
        List<Setmeal> setMeals = setmealService.list();

        Integer num = 0;
        ArrayList<DishCategory> arrayList = new ArrayList<>();

        //3.再根据每个菜品进行统计每个菜品的金额
        for (Category category : categories) {
            for (Setmeal setMeal : setMeals) {
                if (category.getId().equals(setMeal.getCategoryId())){
                    //说明是同一道菜，就将金额加起来
                    num ++;
                }
            }
            //封装的value和Name
            DishCategory dishCategory = new DishCategory();
            dishCategory.setValue(num);
            dishCategory.setName(category.getName());
            arrayList.add(dishCategory);
            //重新将amount置为0
            num = 0;
        }
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("setmealNum",arrayList);
        return hashMap;
    }

    @GetMapping("/cashPerMonth")
    private Map cashPerMonth(){
        //获取当前的月份
        List<String> monthBetween = getMonthBetween("2023-01", "2023-12");

        for (String month : monthBetween) {
            List<Dish> dishList = dishService.list();
            List<Setmeal> setmealList = setmealService.list();
            //if ()

        }

        return null;
    }

    //获取本年份所有的月份
    private static List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    //获取下一个月
    private static String getLastMonthDate(String dataTime) throws ParseException {
        //时间字符串转 LocalDate 类型
        LocalDate today = LocalDate.parse(dataTime);
        //当前月份+（-1）
        today = today.minusMonths(-1);
        //LocalDate日期格式是"YYYY-MM-DD"，只需要用toString()就可以转化成字符串类型
        return today.toString();

    }

}
